package com.rjyb.student_management_platform.controller;

import com.rjyb.student_management_platform.pojo.*;
import com.rjyb.student_management_platform.pojo.Class;
import com.rjyb.student_management_platform.service.TeacherService;
import com.rjyb.student_management_platform.unit.Weekly;
import com.rjyb.student_management_platform.unit.common.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("TeacherController")
public class TeacherController {


    @Autowired
    TeacherService teacherService;


    //老师领导班级
    @RequestMapping("/toselallclass")
    public String toselallclass(HttpSession session, Map map) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        int id = (int) teacher.getTid();
        List<Map> classlist = teacherService.selclass(id);
        map.put("classlist", classlist);
        return "teacher/Class_management";
    }


    //老师查询学生列表
    @RequestMapping("/selestudent")
    public String selestudent(Model model, int cid, HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        int tid = (int) teacher.getTid();
        List<Student> student = teacherService.selestudent(cid,tid);
        model.addAttribute("student", student);
        return "teacher/class";
    }

    //老师注册
    @RequestMapping("toteacher")
    public String toteacher() {
        return "teacher/teacher_enroll";
    }
    @RequestMapping("teacherEnroll")
    public String teacherEnroll(Teacher teacher, Model model,String password1) {
        if(password1.equals(teacher.getPassword())){
        Teacher teacher1 = teacherService.ifTeacher(teacher.getName(), teacher.getJobNumber());

        if (teacher1 != null) {
            if (teacher1.getPassword().equals("未添加")) {
                teacherService.addTeacher(teacher);
                model.addAttribute("yes", "补全过信息成功请登录");
                return "redirect:/login/tologin";
            } else {
                model.addAttribute("no", "你已经补全过信息了，无法再次补全过信息");
            }

        } else {
            model.addAttribute("no", "你的姓名和工号未录入，补全过信息失败");

        }
        }else {
            model.addAttribute("no","未知错误");
        }
        return "teacher/teacher_enroll";
    }

    //查询本人在此班发布过的作业
    @RequestMapping("/selejob")
    public String selejob(HttpSession session, Model model, int cid) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        List<Map> tjob = teacherService.selejob(teacher,cid);
        model.addAttribute("tjob", tjob);
        model.addAttribute("cid", cid);
        return "teacher/Job_posting_details";
    }


    //老师查询作业情况
    @RequestMapping("/seleclassjob")
    public String seleclassjob(int cid, Model model, int jid) {
        //应交人数
        int seleclassjob = teacherService.seleclassjob(cid);
        //已交人数
        int seleclassjob1 = teacherService.seleclassjob1(jid);
        //已批人数
        int seleclassjob2 = teacherService.seleclassjob2(jid);
        //未批人数
        int seleclassjob3 = teacherService.seleclassjob3(jid);
        model.addAttribute("classNum", seleclassjob);
        model.addAttribute("classNum1", seleclassjob1);
        model.addAttribute("classNum2", seleclassjob2);
        model.addAttribute("classNum3", seleclassjob3);

         List<AnswerSheet> list = teacherService.selectclass(jid);
         List<Student> list1=  teacherService.selectclass1(list,cid);
         model.addAttribute("stuname",list1);


        //查询已完成作业类型
        List<Map> jobtybe1 = teacherService.selectjob1(jid,cid);
        model.addAttribute("jobtybe1",jobtybe1);
        //查询已完成作业学生
        List<Map> jobtybe2 = teacherService.selectjob2(jid,cid);
        model.addAttribute("jobtybe2",jobtybe2);
        return "teacher/seleclassjob";
    }
    //按照老师查询班级
    @RequestMapping("selectClass")
    public String selectClass(HttpSession session,Model model){
        List<Class> classList = teacherService.selectClass(session, model);
        model.addAttribute("classList",classList);
        model.addAttribute("stu",null);
        return "teacher/addAttendance";
    }

    //按照班级查询学生
    @RequestMapping("selectStu")
    public String selectStu(Class c,Model model,HttpSession session){
        return teacherService.selectStu(c,model,session);
    }

    //添加考勤
    @RequestMapping("attendanceAdd")
      public String attendanceAdd(Long[] sid,Long cid ,Long classSection,Long[] atRate,HttpSession session,Model model){
        return teacherService.attendanceAdd(sid,cid,classSection,atRate,session,model);
    }

    //去查看考勤
    @RequestMapping("/toCheckattendance")
    public String toCheckattendance(HttpSession session,Model model){
        List<Class> toCheckattendance = teacherService.toCheckattendance(session);
        model.addAttribute("classname",toCheckattendance);
        return "teacher/attendance_table_classname";
    }




    //查看考勤
    @RequestMapping("/Checkattendance")
    public String Checkattendance(Map map,HttpSession session,long cid){
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        int tid = (int) teacher.getTid();
        List<Map> seleclass = teacherService.selclass(tid);
        map.put("seleclass",seleclass);
        map.put("cid",cid);
        map.put("selectattendance",null);

        return "teacher/attendance_tablexzr";
    }


    //条件查询考勤
    @RequestMapping("/Attendance_inquiry")
    public String Attendance_inquiry(Map map,long cid,String time,long class_section,HttpSession session){
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        int tid = (int) teacher.getTid();
       /* List<Map> seleclass = teacherService.selclass(tid);
        map.put("seleclass",seleclass);*/
        List<Map> selectattendance = teacherService.selectattendance(cid,time,class_section,map,session);
        List<Map> selectattendance2 = teacherService.tname(cid,time,class_section,map,session);
        if (selectattendance2.size()!=0){
            Object name = selectattendance2.get(0).get("name");
            map.put("tname",name);
        }
        map.put("cid",cid);
        map.put("time",time);
        map.put("class_section",class_section);
        map.put("selectattendance",selectattendance);

        return "teacher/attendance_tablexzr";
    }

    @RequestMapping("attendanceModify")
    @ResponseBody
    public String attendanceModify(Long class_section,Long sid,String atDate,Long cid,Long atRate){
      //  System.out.println("进来了");
       // System.out.println(atDate);
        return teacherService.attendanceModify(class_section,sid,atDate,cid,atRate);
    }

    //主页本周查询考勤
    @RequestMapping("selectStudentAttendance")
    @ResponseBody
    public List<Integer> selectStudentAttendance(HttpSession session,Model model){
        Teacher teacher =(Teacher)session.getAttribute("teacher");
        long tid = teacher.getTid();
        long type = teacher.getType();
        List<Integer> attDkl = new ArrayList<>();
        //本周时间
        Weekly weekly = new Weekly();
        List<String> weekly1 = weekly.getMondayOfThisWeek();
       // List<Integer> day = new ArrayList<>();
        System.out.println(weekly1);
        if(type==2){
            for (String s : weekly1) {
                double day1 = 0;
                double day = 0;
                for (int i = 1; i <=6 ; i++) {
                    //班导查询本班人数
                    day1=day1+teacherService.selectPeople(tid,s);
                    //班导查询本班本周已到人数
                    day+=teacherService.selectAttendanceHasArrived(tid,s,i);
                    System.out.println(teacherService.selectAttendanceHasArrived(tid, s, i)+"第"+i+"天");
                }
                System.out.println(day+"dasda");
                System.out.println(day1+"dasda");
                if(day==0){
                    attDkl.add(0);
                }else {
                    attDkl.add((int) Math.ceil((day/day1)*100));

                }
                System.out.println(attDkl.toString());
            }
        }else {
            // long tid = 1;
            for (String s : weekly1) {
                Integer attendance = teacherService.selectAttendance(tid, s);
                if (attendance == null) {
                    attendance = 0;
                }
                attDkl.add(attendance);
            }
          //  System.out.println(attDkl);
//        model.addAttribute("list",attendance);
        }
        return attDkl;
    }

    //主页查看本天课程考勤
    @RequestMapping("selectAttendanceForTheDay")
    @ResponseBody
    public List<Integer> selectAttendanceForTheDay(HttpSession session){
        Teacher teacher =(Teacher)session.getAttribute("teacher");
        long tid = teacher.getTid();
        long typy = teacher.getType();
        DateTime date = new DateTime();
        String nyr = date.nowYear();
        System.out.println(nyr);
        List<Integer> list = new ArrayList<>();
        if (typy==2){
            for (int i = 1; i <=6 ; i++) {
                long cs = i;
                Integer list1 = teacherService.selectClassGuideAttendanceForTheDay(tid,nyr,cs);
                if(list1==null){
                    list1 = 0;
                }
                list.add(list1);
            }
            System.out.println(list);
        }else {
            // long tid = 1;
            for (int i = 1; i <= 6; i++) {
                long cs = i;
                Integer list1 = teacherService.selectAttendanceForTheDay(tid, nyr, cs);
                if (list1 == null) {
                    list1 = 0;
                }
                list.add(list1);
            }
            System.out.println(list);
        }
        return list;
    }
}
